<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录的表单 -->
        <el-form
          class="login_form"
          :model="loginForm"
          ref="loginForms"
          :rules="rules"
        >
          <h1>Hello Admin</h1>
          <h2>欢迎管理员登录</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              type="primary"
              size="default"
              @click.enter="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ElNotification } from 'element-plus'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import useUserStore from '@/store/modules/user'
import { getTime } from '@/utils/time'
let userStore = useUserStore()
let $router = useRouter()
let loginForm = reactive({ username: 'admin', password: '123456' })
let loading = ref(false)
let loginForms = ref()
// 登录回调函数
const login = () => {
  loginForms.value
    .validate()
    .then(() => {
      loading.value = true
      userStore
        .userLogin(loginForm)
        .then(() => {
          loading.value = false
          $router.push('/')
          //登录成功提示
          ElNotification({
            type: 'success',
            message: `Hi.${getTime()}好！`,
          })
        })
        .catch(() => {
          loading.value = false
        })
    })
    .catch(() => {
      ElNotification({
        type: 'warning',
        message: '填写数据不合法！',
      })
    })
}

//定义表单校验需要配置对象
const rules = {
  username: [
    {
      required: true,
      min: 3,
      max: 10,
      message: '账号长度至少三位',
      trigger: 'change',
    },
  ],
  password: [
    {
      required: true,
      min: 3,
      max: 15,
      message: '密码长度至少三位',
      trigger: 'change',
    },
  ],
}
</script>
<style lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.png') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
